package com.zinnaworks.smart_cloud_gamepad.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.anymote.Key.Code;
import com.zinnaworks.smart_cloud_gamepad.R;
import com.zinnaworks.smart_cloud_gamepad.protocol.QueuingSender;

public class DualJoystickView extends LinearLayout {
	
	
	public interface JoypadListener {

	    /**
	     * Called when the Dpad was clicked.
	     */
	    void onJoypadClicked();

	    /**
	     * Called when the Dpad was moved in a given direction, and with which
	     * action (pressed or released).
	     *
	     * @param direction the direction in which the Dpad was moved
	     * @param pressed   {@code true} to represent an event down
	     */
	    void onJoypadKeyPress(Code code);
	    void onJoypadPoint(int x, int y);
	  }
	
	
	@SuppressWarnings("unused")
	private static final String TAG = DualJoystickView.class.getSimpleName();
	
	private final boolean D = false;
	private Paint dbgPaint1;

	private JoystickView stickL;
	private JoystickView stickR;
	private JoystickImageButton btn1;
	private JoystickImageButton btn2;
	private JoystickImageButton btn3;
	private JoystickImageButton btn4;

	private View pad;
	
	
	public JoypadListener listener;

	public DualJoystickView(Context context) {
		super(context);
	  	
		stickL = new JoystickView(context);
		stickR = new JoystickView(context);
		btn1 = new JoystickImageButton(context, R.drawable.image_button, "Btn1");
		btn2 = new JoystickImageButton(context, R.drawable.image_button, "Btn2");
		btn3 = new JoystickImageButton(context, R.drawable.image_button, "Btn3");
		btn4 = new JoystickImageButton(context, R.drawable.image_button, "Btn4");
		initDualJoystickView();
	}
	
	public void setJoypadListener(JoypadListener l)
	{
		this.listener = l;
		stickL.setJoypadListener(this.listener);
		stickR.setJoypadListener(this.listener);
		btn1.setJoypadListener(this.listener);
		btn2.setJoypadListener(this.listener);
		btn3.setJoypadListener(this.listener);
		btn4.setJoypadListener(this.listener);
	}

	public DualJoystickView(Context context, AttributeSet attrs) {
		super(context, attrs);
		stickL = new JoystickView(context, attrs);
		
		stickR = new JoystickView(context, attrs);
		btn1 = new JoystickImageButton(context, R.drawable.image_button, "Btn1");
		btn2 = new JoystickImageButton(context, R.drawable.image_button, "Btn2");
		btn3 = new JoystickImageButton(context, R.drawable.image_button, "Btn3");
		btn4 = new JoystickImageButton(context, R.drawable.image_button, "Btn4");
		
		initDualJoystickView();
	}

	private void initDualJoystickView() {
		setOrientation(LinearLayout.HORIZONTAL);
		if ( D ) {
			dbgPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
			dbgPaint1.setColor(Color.CYAN);
			dbgPaint1.setStrokeWidth(1);
			dbgPaint1.setStyle(Paint.Style.STROKE);
		}
		
		pad = new View(getContext());
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		removeView(stickL);
		removeView(stickR);
		removeView(btn1);
		removeView(btn2);
		removeView(btn3);
		removeView(btn4);
		

		float padW = getMeasuredWidth()-(getMeasuredHeight()*7);
		int joyWidth = (int) ((getMeasuredWidth()-padW)/7);
		LayoutParams joyLParams = new LayoutParams(joyWidth,getMeasuredHeight());
		
//		RelativeLayout.LayoutParams paramsOn;
//		paramsOn = (RelativeLayout.LayoutParams)btn1.getLayoutParams();
//		paramsOn.leftMargin = paramsOn.leftMargin + 100;
		
		stickL.setLayoutParams(joyLParams);
		stickR.setLayoutParams(joyLParams);
		btn1.setLayoutParams(joyLParams);
		btn2.setLayoutParams(joyLParams);
		btn3.setLayoutParams(joyLParams);
		btn4.setLayoutParams(joyLParams);
		
		
		stickL.TAG = "L";
		stickR.TAG = "R";
		btn1.TAG = "BTN1";
		btn1.TAG = "BTN2";
		btn1.TAG = "BTN3";
		btn1.TAG = "BTN4";
		
		
		
		stickL.setPointerId(JoystickView.INVALID_POINTER_ID);
		stickR.setPointerId(JoystickView.INVALID_POINTER_ID);
		btn1.setPointerId(JoystickView.INVALID_POINTER_ID);
//		btn2.setPointerId(JoystickView.INVALID_POINTER_ID);
//		btn3.setPointerId(JoystickView.INVALID_POINTER_ID);
//		btn4.setPointerId(JoystickView.INVALID_POINTER_ID);

		addView(stickL);

		ViewGroup.LayoutParams padLParams = new ViewGroup.LayoutParams((int) padW,getMeasuredHeight());
		removeView(pad);
		pad.setLayoutParams(padLParams);
		addView(pad);
		
		addView(btn1);
//		addView(btn2);
//		addView(btn3);
//		addView(btn4);
		addView(stickR);
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		stickR.setTouchOffset(stickR.getLeft(), stickR.getTop());
	}
	
	public void setAutoReturnToCenter(boolean left, boolean right) {
		stickL.setAutoReturnToCenter(left);
		stickR.setAutoReturnToCenter(right);
	}
	
	public void setOnJostickMovedListener(JoystickMovedListener left, JoystickMovedListener right) {
		stickL.setOnJostickMovedListener(left);
		stickR.setOnJostickMovedListener(right);
	}
	
	public void setOnJostickClickedListener(JoystickClickedListener left, JoystickClickedListener right) {
		stickL.setOnJostickClickedListener(left);
		stickR.setOnJostickClickedListener(right);
	}
	
	public void setYAxisInverted(boolean leftYAxisInverted, boolean rightYAxisInverted) {
		stickL.setYAxisInverted(leftYAxisInverted);
		stickL.setYAxisInverted(rightYAxisInverted);
	}

	public void setMovementConstraint(int movementConstraint) {
		stickL.setMovementConstraint(movementConstraint);
		stickR.setMovementConstraint(movementConstraint);
	}

	public void setMovementRange(float movementRangeLeft, float movementRangeRight) {
		stickL.setMovementRange(movementRangeLeft);
		stickR.setMovementRange(movementRangeRight);
	}

	public void setMoveResolution(float leftMoveResolution, float rightMoveResolution) {
		stickL.setMoveResolution(leftMoveResolution);
		stickR.setMoveResolution(rightMoveResolution);
	}

	public void setUserCoordinateSystem(int leftCoordinateSystem, int rightCoordinateSystem) {
		stickL.setUserCoordinateSystem(leftCoordinateSystem);
		stickR.setUserCoordinateSystem(rightCoordinateSystem);
	}
	
	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		if (D) {
			canvas.drawRect(1, 1, getMeasuredWidth()-1, getMeasuredHeight()-1, dbgPaint1);
		}
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		
    	boolean l = stickL.dispatchTouchEvent(ev);
    	boolean r = stickR.dispatchTouchEvent(ev);
    	boolean b1 = btn1.dispatchTouchEvent(ev);
    	boolean b2 = btn2.dispatchTouchEvent(ev);
    	boolean b3 = btn3.dispatchTouchEvent(ev);
    	boolean b4 = btn4.dispatchTouchEvent(ev);
    	
    	return l || r || b1 || b2 || b3 || b4;
	}
	
//	@Override
//	public boolean onTouchEvent(MotionEvent ev) {
////		Log.d("onTouchEvent", ev.toString());
//    	boolean l = stickL.onTouchEvent(ev);
//    	boolean r = stickR.onTouchEvent(ev);
//    	boolean b1 = btn1.onTouchEvent(ev);
//    	
//    	return l || r || b1;
//	}
	private class MissingSenderToaster
	implements QueuingSender.MissingSenderListener {
		private long lastToastTime;


		public void onMissingSender() {
			//     if (System.currentTimeMillis() - lastToastTime > MIN_TOAST_PERIOD) {
			//       lastToastTime = System.currentTimeMillis();
			//       showMessage(R.string.sender_missing);
			//     }
		}
	}
}
